import matplotlib.pyplot as plt
import numpy as np

def read_data():
    # 城市名数组
    city_name = []
    # 城市坐标数组
    city_condition = []
    # 通过open的方法打开txt文件输入数据
    with open('快递公司送货策略/data.txt', 'r', encoding='utf-8') as f:
        lines = f.readlines()  # 读取每一行数据
        # 处理每一行数据
        for line in lines:
            line = line.split('\n')[0]
            # 数据通过空格和逗号分离出坐标x和坐标y以及城市名称
            line = line.split(',')
            # print(line)
            # 将分离出的城市名和城市地址放入数组中
            city_name.append(line[0])
            city_condition.append([float(line[1]), float(line[2])])
    # 因为格式原因而转换成numpy数组
    city_condition = np.array(city_condition)
    # 返回处理后的结果
    return city_name, city_condition


def paint():
    _names,city_condition = read_data()
    result_path = [
        [0, 4, 6, 2, 0],
        [0, 16, 17, 5, 0]
    ]

    for i in range(len(result_path)):
        xs = list()
        ys = list()
        for j in result_path[i]:
            # 画注解

            plt.annotate('', [city_condition[j, 0], city_condition[j, 1]])
            xs.append(city_condition[j, 0])
            ys.append(city_condition[j, 1])
            plt.plot(xs, ys, '-o')


    # 绘图
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.show()



if __name__ == '__main__':
    paint()
